set linesize 255 

global path = "****"
local name= "costs"
local date = "051420"
set matsize 11000
cd "$path"

/* NHE per person expenses 2014-2018*/
matrix schip=[2239,2448,2581,2640,2636]
matrix mcaid=[7592,7835,7948,8041,8201]
matrix esi=[5182,5372,5617,5802,6103]



global logname = "${path}`name'_`date'.log"
log using "$logname", replace

forvalues i=2017/2018{

use Data/recon_penalty_`i',clear

collapse (sum) replay add_ptc penalty ,by(tin)

merge 1:1 tin using Data/f1095_`i',gen(mrec)

foreach var of varlist replay add_ptc penalty aptctotal slcsptotal premtotal {
replace `var'=0 if `var'==.
}


gen ptc=max(aptctotal +add_ptc -replay,0)



/* matrix of results */


matrix costs_`i'=J(5,5,.)
matrix rowname costs_`i'=PTC costshare penalty Medicaid ESI

matrix colname costs_`i'=spending months_cov marginal_effect N_treat costs




merge 1:m tin using Data/penalty_sample,keep(1 3) keepusing(exchange`i' magi_fpl`i' dep_tin esi`i' covered`i' medicaid`i' state age_2017 notall16) gen(mcsr)
merge m:1 state using Data/medicaid_costs, gen(merge_mcaid)


snapshot save /* 1 */

/* PTC*/
count if dep_tin==tin&age_2017>=45&age_2017<=64&notall16==1

qui sum ptc if tin==dep_tin&age_2017>=45&age_2017<=64&notall16==1

matrix costs_`i'[1,1]=r(sum)

qui sum exchange`i' if age_2017>=45&age_2017<=64&notall16==1


matrix costs_`i'[1,2]=r(sum)

reg exchange`i' treatment if age_2017>=45&age_2017<=64&notall16==1

matrix b=e(b)
matrix costs_`i'[1,3]=b[1,1]
qui sum exchange`i' if treatment==1&age_2017>=45&age_2017<=64&notall16==1

matrix costs_`i'[1,4]=r(N)


/*CSR */
/*
100-150% FPL = 94% Actuarial Value (CSR 94)
150-200% FPL = 87% Actuarial Value (CSR 87) 
200-250% FPL = 73% Actuarial Value (CSR 73)
*/


gen csr_p=slcsptotal *(.94/.7-1) if magi_fpl`i' <1.5&tin==dep_tin&age_2017>=45&age_2017<=64&notall16==1

replace csr_p =slcsptotal *(.87/.7-1) if magi_fpl`i' >=1.5&magi_fpl`i' <2&tin==dep_tin&age_2017>=45&age_2017<=64&notall16==1

replace csr_p =slcsptotal *(.73/.7-1) if magi_fpl`i' >=2.0&magi_fpl`i' <2.5&tin==dep_tin&age_2017>=45&age_2017<=64&notall16==1


qui sum csr_p if tin==dep_tin&age_2017>=45&age_2017<=64&notall16==1

matrix costs_`i'[2,1]=r(sum)*.9 /* 90% of people in exchanges take a silver plan that is eligible for CSR */


qui sum exchange`i' if magi_fpl`i' <2.5&age_2017>=45&age_2017<=64&notall16==1

matrix costs_`i'[2,2]=r(sum)

reg exchange`i' treatment if magi_fpl`i'<2.5&age_2017>=45&age_2017<=64&notall16==1

matrix b=e(b)
matrix costs_`i'[2,3]=b[1,1]

qui sum exchange`i' if magi_fpl`i' <2.5&treatment==1&age_2017>=45&age_2017<=64&notall16==1

matrix costs_`i'[2,4]=r(N)


/* Penalty */

gen uninsurance=12-covered`i'
qui sum penalty if tin==dep_tin&age_2017>=45&age_2017<=64&notall16==1

matrix costs_`i'[3,1]=r(sum)

qui sum uninsurance if age_2017>=45&age_2017<=64&notall16==1

matrix costs_`i'[3,2]=r(sum)
reg uninsurance treatment if age_2017>=45&age_2017<=64&notall16==1




matrix b=e(b)
matrix costs_`i'[3,3]=-b[1,1]
reg penalty treatment if age_2017>=45&age_2017<=64&notall16==1

qui sum uninsurance if treatment==1&age_2017>=45&age_2017<=64&notall16==1

matrix costs_`i'[3,4]=r(N)

/* Medicaid */

gen caidcosts=cost_adult*medicaid`i'/12 if age_2017>18&age_2017>=45&age_2017<=64&notall16==1

replace caidcosts=cost_child*medicaid`i'/12 if age_2017<=18&age_2017>=45&age_2017<=64&notall16==1


qui sum caidcosts if age_2017>=45&age_2017<=64&notall16==1


matrix costs_`i'[4,1]=r(sum)*mcaid[1,`i'-2014]/mcaid[1,1]

qui sum medicaid`i' if age_2017>=45&age_2017<=64&notall16==1

matrix costs_`i'[4,2]=r(sum)

reg medicaid`i' treatment if age_2017>=45&age_2017<=64&notall16==1



matrix b=e(b)
matrix costs_`i'[4,3]=b[1,1]

qui sum medicaid`i' if treatment==1&age_2017>=45&age_2017<=64&notall16==1

matrix costs_`i'[4,4]=r(N)


/* ESI */
if `i'==2017{
gen esicosts=esi[1,`i'-2014]*esi`i'/12 *(.15+.02)
}
if `i'==2018{
gen esicosts=esi[1,`i'-2014]*esi`i'/12 *(.12+.02)
}

qui sum esicosts if age_2017>=45&age_2017<=64&notall16==1


matrix costs_`i'[5,1]=r(sum)

qui sum esi`i' if age_2017>=45&age_2017<=64&notall16==1

matrix costs_`i'[5,2]=r(sum)

reg esi`i' treatment if age_2017>=45&age_2017<=64&notall16==1



matrix b=e(b)
matrix costs_`i'[5,3]=b[1,1]

qui sum esi`i' if treatment==1&age_2017>=45&age_2017<=64&notall16==1

matrix costs_`i'[5,4]=r(N)


forvalues k=1/5{
matrix costs_`i'[`k',5]=costs_`i'[`k',1]/costs_`i'[`k',2]*costs_`i'[`k',3]*costs_`i'[`k',4]



}
}


* Number of lives saved */

use Data/penalty_sample,clear

reg dead1718 treatment if notall16 ==1&age_2017 >=45&age_2017 <=64

matrix b=e(b)

local coef=-b[1,1]
sum treatment if treatment==1&notall16 ==1&age_2017 >=45&age_2017 <=64


local num=r(N)

local saved=`coef'*`num'

display "number lives saved=`saved'"

matrix list costs_2017,format(%15.3fc)
matrix list costs_2018,format(%15.3fc)

clear
svmat costs_2017
svmat costs_2018

save "$path/vsl_table_`date'.dta", replace

log close



